package io.netty.server.discard;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

/** 
 * @author  lianlb 
 * @date 2017年11月7日  下午6:49:20 
 * @version 1.0 
 */
public class SelfDefineDecoder extends ByteToMessageDecoder {
	
	private String tip = "********** 缓存区  ******** ";

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		System.out.println(in.toString());
//		while (in.isReadable()) {
//			System.out.println((char)in.readByte());
//		}
		String msg = "";
		byte[] dst = new byte[in.readableBytes()];
		int bytesLen = in.readableBytes();
		in.markReaderIndex();
		if (bytesLen == 0) {
			System.out.println(tip + "没有数据");
		} else if(bytesLen < 10) {
			in.readBytes(dst);
			msg = new String(dst);
			System.out.println(tip + "心跳包：" + msg);
			out.add(msg);
		} else if(bytesLen < 50) {
			in.resetReaderIndex();
		} else {
			in.readBytes(dst);
			msg = new String(dst);
			System.out.println(tip + "业务数据：" + msg);
			if (msg.contains("$_")) {
				out.add(msg);
			}
			
		}
	}

}
